<template>
  <div class="login">
    <div class="box">
      <div class="left"></div>
      <div class="right">
        <div>
          <h2>系统登录</h2>
          <div @keydown.enter="onSubmit">
            <a-form
              ref="formRef"
              :model="formState"
              :rules="rules"
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
            >
              <a-form-item label="账户" name="name">
                <a-input
                  v-model:value="formState.name"
                  placeholder="请输入用户名"
                />
              </a-form-item>
              <a-form-item label="密码" name="password">
                <a-input-password
                  v-model:value="formState.password"
                  placeholder="请输入密码"
                />
              </a-form-item>

              <a-form-item :wrapper-col="{ offset: 2 }">
                <a-button type="primary" @click="onSubmit">登录</a-button>
              </a-form-item>
            </a-form>
          </div>
          <div style="padding-left: 7%; cursor: pointer">
            <span style="color: #000" @click="jump">Or 注册用户</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import axios from 'axios'
import { defineComponent, reactive, ref } from 'vue'
// 获取vuex的数据
import useStore from 'vuex'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
export default defineComponent({
  setup(props, context) {
    let state = reactive({
      width: Math.ceil(Math.random() * 20)
    })
    let router = useRouter()
    let jump = () => {
      router.push('/register')
    }
    const formRef = ref()
    const formState = reactive({
      name: undefined,
      password: undefined
    })
    const rules = {
      name: {
        required: true,
        message: '请输入用户名！'
      },
      password: {
        required: true,
        message: '请输入密码！'
      }
    }
    const onSubmit = () => {
      formRef.value
        .validate()
        .then(() => {
          console.log('values', formState)
          let data = { username: formState.name, password: formState.password }
          axios.post('http://localhost:7001/api/login', data).then((res) => {
            if (res.data.status === 200) {
              router.push('/staging')
              message.success(res.data.message)
              console.log(res.data.user.role, 'role')
              console.log(res.data.user.username, 'role')
              localStorage.setItem('role', res.data.user.role)
              localStorage.setItem('username', res.data.user.username)
            } else {
              message.error(res.data.message)
            }
          })
        })
        .catch((error: never) => {
          console.log('error', error)
        })
    }
    return {
      state,
      formRef,
      labelCol: { span: 3 },
      wrapperCol: { span: 19 },
      formState,
      rules,
      onSubmit,
      jump
    }
  }
})
</script>

<style lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background: #e7eaee;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;

  .box {
    width: 80%;
    height: 85%;
    background: #fff;
    display: flex;
    z-index: 1000;

    .left {
      width: 50%;
      height: 100%;
      background: url(../assets/6.png);
      background-size: 100% 100%;
    }

    .right {
      width: 50%;
      height: 100%;
      background: #f7fafb;
      padding-top: 13%;

      h2 {
        padding-left: 20px;
      }

      button {
        width: 91%;
        margin-top: 5px;
      }

      .anticon svg {
        display: none;
      }
    }
  }
}
</style>
